package com.guaju.yueshangclothing.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.guaju.yueshangclothing.BR;
import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.adapter.NoneCommonRecyclerViewAdapter;
import com.guaju.yueshangclothing.base.BaseFragment;
import com.guaju.yueshangclothing.bean.Goods;
import com.guaju.yueshangclothing.databinding.FragmentRecommendBinding;
import com.guaju.yueshangclothing.utils.ScreenUtils;
import com.guaju.yueshangclothing.viewmodel.home.RecommandVM;

import java.util.ArrayList;

/**
 * Created by guaju on 2018/9/28.
 */

public class RecommendFragment extends BaseFragment<FragmentRecommendBinding> {

    private static final String TYPE = "RecommendFragment";
    ArrayList sliderList = new ArrayList<>();
    ArrayList goodsList = new ArrayList<>();
    private boolean mIsPrepared;
    private boolean mIsFirst = true;

    @Override
    public int setContent() {
        return R.layout.fragment_recommend;
    }


    public static RecommendFragment newInstance(String param) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle args = new Bundle();
        args.putString(TYPE, param);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 准备就绪
        mIsPrepared = true;
        loadData();
        showContentView();
        //回到顶部处理
        toTop();

    }

    private void toTop() {
        bindingView.scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (Math.abs(scrollY) > ScreenUtils.getScreenHeight(getActivity())) {
                    //说明滑动超过了屏幕
                    bindingView.totop.setVisibility(View.VISIBLE);
                } else {
                    bindingView.totop.setVisibility(View.INVISIBLE);
                }

            }
        });
        bindingView.totop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindingView.scrollView.fling(0);
                bindingView.scrollView.smoothScrollTo(0,0);
            }
        });
    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        mIsFirst = false;
        bindingView.linearlayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadCustomData();
            }
        }, 500);
    }

    private void loadCustomData() {
        sliderList.add("http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        sliderList.add("http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        sliderList.add("http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        sliderList.add("http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        sliderList.add("http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        RecommandVM recommandVM = new RecommandVM();
        recommandVM.setSliderList(sliderList);


        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goodsList.add(goods);
        }
        NoneCommonRecyclerViewAdapter<Goods> goodsCommonRecyclerViewAdapter =
                new NoneCommonRecyclerViewAdapter<Goods>(getActivity(), getLayoutInflater(), R.layout.item_goods, BR.goods, goodsList);
        bindingView.xrecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2) {
                                                       @Override
                                                       public boolean canScrollVertically() {
                                                           return false;
                                                       }
                                                   }
        );
        bindingView.xrecyclerview.setAdapter(goodsCommonRecyclerViewAdapter);


        bindingView.setViewmodel(recommandVM);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        if (bindingView!=null&&bindingView.linearlayout!=null){
        bindingView.linearlayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                bindingView.slider.startAutoCycle();
            }
        }, 500);
        }
    }

    @Override
    protected void onInvisible() {
        // 不可见时轮播图停止滚动
        if (bindingView != null && bindingView.slider != null) {
            bindingView.slider.stopAutoCycle();
        }
    }
}
